package org.xa3apm.vote.hibernate.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "tProvince")
@NamedQueries({
		@NamedQuery(name = "Province.getById", query = "SELECT P FROM Province P WHERE P.id = :id"),
		@NamedQuery(name = "Province.getAll", query = "SELECT P FROM Province P") })
public class Province implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8805536081832432275L;
	@Id
	@GeneratedValue
	@Column(name = "id", nullable = false, unique = true, updatable = false)
	private Long id;
	@Column(name = "name")
	private String name;
	@OneToMany(mappedBy = "province", fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL)
	private Collection<Municipality> municipalities;

	public Province() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param provinceId
	 * @param provinceName
	 * @param municipalities
	 */
	public Province(String pName, Collection<Municipality> municipalities) {
		this.name = pName;
		this.municipalities = municipalities;
	}

	/**
	 * @return the provinceId
	 */
	public final Long getId() {
		return id;
	}

	/**
	 * @param provinceId
	 *            the provinceId to set
	 */
	public final void setProvinceId(Long pId) {
		this.id = pId;
	}

	/**
	 * @return the municipalities
	 */
	public final Collection<Municipality> getMunicipalities() {
		if (municipalities == null) {
			municipalities = new ArrayList<Municipality>();
		}
		return municipalities;
	}

	/**
	 * @param municipalities
	 *            the municipalities to set
	 */
	public final void setMunicipalities(Collection<Municipality> municipalities) {
		this.municipalities = municipalities;
	}

	public String getName() {
		return name;
	}

	public void setName(String pName) {
		this.name = pName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((municipalities == null) ? 0 : municipalities.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Province))
			return false;
		Province other = (Province) obj;
		if (municipalities == null) {
			if (other.municipalities != null)
				return false;
		} else if (!municipalities.equals(other.municipalities))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		// return just id and name
		return id + ", " + name;
	}

}